#include <ctype.h>
#include <stdio.h>
#include <limits.h>

enum State
{
	INVALE,
	VALE
}state = INVALE;

int myatoi(char* s)
{
	int flag = 1;
	if (s == NULL || *s =='\0') return 0;

	while (isspace(*s))  { s++; }
	if (*s == '\0') return 0;

	if (*s == '+') {
		s++;
	} else if (*s == '-') {
		flag = -1;
		s++;
	}

	long long n = 0;
	while (isdigit(*s))
	{
		n = n * 10 + (*s - '0');
		if (n > INT_MAX) return 0;
		s++;
	}
	if (isalpha(*s)) return 0;

	state = VALE;
	return (int)(flag * n);
}

int main()
{
	char* s = "   66 a";
	printf("atoi : %d\n\n", atoi(s));
	int ret = myatoi(s);
	if (state == VALE)
	{
		printf("合法的%d\n", ret);
	}
	else
	{
		printf("非法的%d\n", ret);
	}
	return 0;
}